package pe.hongi.frist;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class SavePdf_DBAdapter {

    private static final String DATABASE_NAME = "save_pdf.db";
    private static final String DATABASE_TABLE = "save_pdf_tbl_manger";
    private static final int    DATABASE_VERSION = 1;

    
    private static final String DATABASE_TABLE_CREATE =
        "CREATE TABLE " + DATABASE_TABLE +
        " (id INTEGER PRIMARY KEY AUTOINCREMENT," +
        "url TEXT NOT NULL," +
        "path TEXT NOT NULL," +
        "pdf_id  TEXT NOT NULL);";
    
    
    private static final String DATABASE_TABLE_DROP =
        "DROP TABLE IF EXISTS " + DATABASE_TABLE;

    private SQLiteDatabase mDb; 		
    private DatabaseHelper mDbHelper; 	
    private Context context;
    
    public SavePdf_DBAdapter(Context _context) {
        context = _context;

       
        mDbHelper = new DatabaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    
    private class DatabaseHelper extends SQLiteOpenHelper {

        public DatabaseHelper(
            Context context, String name, CursorFactory factory, int version) {

            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

       
        public void onCreate(SQLiteDatabase _db) {
            _db.execSQL(DATABASE_TABLE_CREATE);
        }
 
        public void onUpgrade(SQLiteDatabase _db, int oldVersion, int newVersion) {
            Log.w("MY_TAG", "Upgrading DB from version" + oldVersion + " to" +
                newVersion + ", which will destroy all old data");

            _db.execSQL(DATABASE_TABLE_DROP);
            onCreate(_db);
        }

    }

    
    public SavePdf_DBAdapter open() throws SQLException {
     
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

   
    public void close() {
        
        mDb.close();
    }

 
    
    
    
    public long insertEntry(String url, String path, String pdf_id) {
        ContentValues values = new ContentValues();
        values.put("url", url);
        values.put("path", path);
        values.put("pdf_id", pdf_id);
        return mDb.insert(DATABASE_TABLE, null, values);
    }

    

    
    
    public boolean updateEntry(long rowID, String url, String path, String pdf_id) {
        ContentValues values = new ContentValues();
        values.put("url", url);
        values.put("path", path);
        values.put("pdf_id", pdf_id);
        return mDb.update(DATABASE_TABLE, values, "id=" + rowID, null) > 0;
    }
    
    

    public boolean deleteEntry(long rowID) {
        return mDb.delete(DATABASE_TABLE, "id=" + rowID, null) > 0;
    }

   
    public void deleteEntry_for_url(String url) {
    	mDb.execSQL("DELETE FROM " + DATABASE_TABLE + " WHERE url=" + "'" + url + "'");
    }
    
    
    public void deleteEntry_for_pdf_id(String pdf_id) {
    	mDb.execSQL("DELETE FROM " + DATABASE_TABLE + " WHERE pdf_id=" + "'" + pdf_id + "'");
    }

   
    
    
    public int countEntry_for_pdf_id(String pdf_id) { 

    	String Sql = "SELECT * FROM " + DATABASE_TABLE + " WHERE pdf_id=" + "'" + pdf_id + "'";
    	Cursor	pCur = mDb.rawQuery(Sql, null);
    	int Count_Item = pCur.getCount();
    	pCur.close();
    	return Count_Item; 
    }
    
   
   
   public Cursor getEntry_for_pdf_id(String pdf_id)throws SQLException
	{
		Cursor mCursor = mDb.query(true, DATABASE_TABLE,
				new String[]{"id", "url", "path", "pdf_id"},
				"pdf_id=" + "'" + pdf_id + "'",
				null, null, null, null, null);
		
		if(mCursor != null) mCursor.moveToFirst();

        return mCursor;
		
	}
   
    public Cursor getAllEntries() {
        return mDb.query(DATABASE_TABLE, new String[]{"id", "id", "url", "path", "pdf_id"},
                               null, null, null, null, null, null);
    }


}
